const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getData(data);
				break;
			case 'queryById':
				res.data = await getDataById(data);
				break;
			case 'add':
				res = await addData(data);
				break;
			case 'edit':
				res = await editData(data);
				break;
			case 'del':
				res = await delData(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}

/**
 * 获取列表数据
 */
async function getData(data) { 
	if (data.state == 0||data.state =='') {
		const result = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/')
			})
			.skip((data.page-1)*data.limit)
			.limit(data.limit)
			.orderBy('updatetime', 'desc')
			.get()
		const resultTotal = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/')
			})
			.count()
		let res = {
			total:resultTotal.total,
			records:result.data
		}
		return res
	} else if (data.state == 1) {
		const result = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/'),
				startDateSt: dbCmd.gt(new Date().getTime())
			})
			.skip((data.page-1)*data.limit)
			.limit(data.limit)
			.orderBy('updatetime', 'desc')
			.get()
		const resultTotal = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/'),
				startDateSt: dbCmd.gt(new Date().getTime())
			})
			.count()
		let res = {
			total:resultTotal.total,
			records:result.data
		}
		return res
	} else if (data.state == 2) {
		const result = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/'),
				startDateSt: dbCmd.lte(new Date().getTime()),
				endDateSt: dbCmd.gte(new Date().getTime())
			})
			.skip((data.page-1)*data.limit)
			.limit(data.limit)
			.orderBy('updatetime', 'desc')
			.get()
		const resultTotal = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/'),
				startDateSt: dbCmd.lte(new Date().getTime()),
				endDateSt: dbCmd.gte(new Date().getTime())
			})
			.count()
		let res = {
			total:resultTotal.total,
			records:result.data
		}
		return res
	} else if (data.state == 3) {
		const result = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/'),
				endDateSt: dbCmd.lt(new Date().getTime())
			})
			.skip((data.page-1)*data.limit)
			.limit(data.limit)
			.orderBy('updatetime', 'desc')
			.get()
		const resultTotal = await db.collection('pintuan')
			.where({
				_id: dbCmd.neq('null'),
				name: eval('/' + data.name + '/'),
				endDateSt: dbCmd.lt(new Date().getTime())
			})
			.count()
		let res = {
			total:resultTotal.total,
			records:result.data
		}
		return res
	}
}
/**
 * 获取数据通过ID
 */
async function getDataById(data) {
	var result = await db.collection('pintuan').doc(data._id).get();
	return result.data
}
/**
 * 添加数据
 */
async function addData(data) {
	var res = {}
	const result = await db.collection('pintuan').add({
		name: data.name,
		startDate: data.startDate,
		startDateSt: data.startDateSt,
		endDate: data.endDate,
		endDateSt: data.endDateSt,
		peopleNum: data.peopleNum,
		effectiveTime: data.effectiveTime,
		goodsId: data.goodsId,
		allOrder:0,  //成交订单数
		allPeople:0, //成交人数
		allMoney:0,  //成交钱数
		updatetime: data.updatetime
	})
	//更新商品拼团信息
	const result2 = await db.collection('goods').doc(data.goodsId).update({
		proData: data.goodsData.proData,
		updatetime: data.updatetime
	})
	res.code = 200;
	res.data = result.data
	return res;
}
/**
 * 更新数据
 */
async function editData(data) {
	var res = {}
	const result = await db.collection('pintuan').doc(data._id).update({
		name: data.name,
		startDate: data.startDate,
		startDateSt: data.startDateSt,
		endDate: data.endDate,
		endDateSt: data.endDateSt,
		peopleNum: data.peopleNum,
		effectiveTime: data.effectiveTime,
		goodsId: data.goodsId,
		updatetime: data.updatetime
	})
	//更新商品拼团信息
	const result2 = await db.collection('goods').doc(data.goodsId).update({
		proData: data.goodsData.proData,
		updatetime: data.updatetime
	})
	res.code = 200;
	res.data = result.data
	return res;
}
/**
 * @param {Object} data
 * 删除数据
 */
async function delData(data) {
	var res = {}
	var tmp = await db.collection('pintuan').doc(data._id).remove()
	if (tmp.deleted === 1) {
		res.code = 200
		res.msg = '删除成功'
	} else {
		res.code = 402
		res.msg = '删除失败'
	}
	return res
}
